package menu.lt.pers.menu;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import menu.lt.pers.menu.Bean.UserBean;
import menu.lt.pers.menu.dao.UserDao;

public class UserInfoActivity extends FragmentActivity {
    private UserBean user;
    private TextView tv_name;
    private RelativeLayout relative_nick;
    private EditText et_nick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        init();
    }

    private void init() {
        tv_name = findViewById(R.id.tv_nick);
        relative_nick = findViewById(R.id.relative_nick);

        Intent intent = getIntent();
        user = intent.getParcelableExtra("user");
        if (user.getUsername() == "" || user.getUsername() == null){ tv_name.setText("默认用户名"); }
        else { tv_name.setText(user.getUsername()); }
        //给昵称添加点击事件的监听器
        relative_nick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch(which){
                            case Dialog.BUTTON_POSITIVE:
                                UserDao dao = new UserDao(UserInfoActivity.this);
                                user.setUsername(et_nick.getText().toString());
                                dao.update(user);
                                tv_name.setText(user.getUsername());
                                break;
                            case Dialog.BUTTON_NEGATIVE:
                                break;
                            case Dialog.BUTTON_NEUTRAL:
                                break;
                        }
                    }
                };

                AlertDialog.Builder builder = new AlertDialog.Builder(UserInfoActivity.this);
                View update_data  = LayoutInflater.from(UserInfoActivity.this).inflate(R.layout.update_data,null);
                et_nick = update_data.findViewById(R.id.et_nick);
                if (user.getUsername() == null || user.getUsername() == ""){
                    et_nick.setText("默认用户名");
                } else {
                    et_nick.setText(user.getUsername());
                }
                builder.setView(update_data);
                builder.setTitle("昵称");
                //点击对话框以外的区域是否让对话框消失
                builder.setCancelable(true);
                //设置正面按钮
                builder.setPositiveButton("确认",dialogOnclicListener);
                builder.setNegativeButton("取消", dialogOnclicListener);
                builder.show();
            }
        });



    }
}
